package com.ssy.lingxi.contract.config;

import com.ssy.lingxi.common.constant.Constants;
import com.ssy.lingxi.component.redis.lock.LockTemplate;
import com.ssy.lingxi.component.redis.service.IRedisUtils;
import com.ssy.lingxi.contract.esign.EsignProperties;
import com.ssy.lingxi.contract.esign.api.AuthToken;
import com.ssy.lingxi.contract.esign.api.SignApi;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(EsignProperties.class)
public class EsignConfig {

    private final EsignProperties properties;
    private final LockTemplate lockTemplate;

    public EsignConfig(EsignProperties properties, LockTemplate lockTemplate) {
        this.properties = properties;
        this.lockTemplate = lockTemplate;
    }

    @Bean
    public SignApi signApi(IRedisUtils redisUtils) {
        // 缓存
        RedisEsignCache esignCache = new RedisEsignCache(redisUtils, Constants.REDIS_USER_INDEX);
        // 锁
        RedisEsignLock esignLock = new RedisEsignLock(lockTemplate);
        // 认证API
        AuthToken authToken = new AuthToken(esignLock, esignCache, properties);

        // token管理器
        return new SignApi(authToken, properties);
    }


}
